package com.android.love.view;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.love.R;

/**
 * 循环滚动切换图片(支持带标题,不带标题传null即可),自带适配器 ArrayList<ImageView> images 图片集合
 * ArrayList<View> dots 一些点 TextView title 显示标题的TextView String[] titles 标题
 * OnPagerClickCallback onPagerClickCallback 处理page被点击的回调接口
 * 
 * @author lxj
 * 
 */
public class RollViewPager extends ViewPager {
	private int currentItem;
	private ScheduledExecutorService scheduledExecutor;
	private ArrayList<ImageView> images;// 图片
	private ArrayList<View> dots;// 点
	private TextView title;// 每个图片的标题
	private String[] titles;
	private OnPagerClickCallback onPagerClickCallback;

	// 用于更新UI
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// 指定显示的页面
			RollViewPager.this.setCurrentItem(currentItem);
		}

	};

	/**
	 * 
	 * @param context
	 * @param images
	 *            图片集合
	 * @param dots
	 *            一些点
	 * @param titles
	 *            标题数组
	 * @param title
	 *            显示标题的TextView
	 * @param onPagerClickCallback
	 *            处理page被点击的回调接口
	 */
	public RollViewPager(Context context, ArrayList<ImageView> images,
			ArrayList<View> dots, String[] titles, TextView title,
			OnPagerClickCallback onPagerClickCallback) {
		super(context);
		this.images = images;
		this.dots = dots;
		this.title = title;
		this.titles = titles;
		this.onPagerClickCallback = onPagerClickCallback;

		// 设置设配器
		this.setAdapter(new ViewPagerAdapter());

		if (title != null && titles != null && titles.length > 0)
			title.setText(titles[0]);// 默认显示第一张
		this.setOnPageChangeListener(new MyOnPageChangeListener());

		// timer
		scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
		/*
		 * 指定时间来执行所指定的任务 command 任务 initialDelay 第一次启动任务时，设置的延迟启动时间 period
		 * 指定重复执行的间隔时间 unit 指定时间单位
		 * 
		 * 第一次启动任务时，设置的延迟启动时间2秒，每间隔2秒执行一次任务(更换显示图片)
		 */
		scheduledExecutor.scheduleAtFixedRate(new ViewPagerTask(), 2, 2,
				TimeUnit.SECONDS);

	}

	class MyOnPageChangeListener implements OnPageChangeListener {
		int oldPosition = 0;

		@Override
		public void onPageSelected(int position) {
			currentItem = position;
			if (title != null)
				title.setText(titles[position]);
			if (dots != null && dots.size() > 0) {
				dots.get(position).setBackgroundResource(R.drawable.jiaodian2);
				dots.get(oldPosition).setBackgroundResource(
						R.drawable.jiaodian1);
			}
			oldPosition = position;
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	}

	/**
	 * 更新currentItem的任务
	 * 
	 * @author lxj
	 * 
	 */
	public class ViewPagerTask implements Runnable {
		@Override
		public void run() {
			// 处理
			currentItem = (currentItem + 1) % images.size();
			// Handler 处理页面图片的改变
			// handler.sendEmptyMessage(0);
			Log.d("RollViewPager", "当前是第几个:" + currentItem);
			handler.obtainMessage().sendToTarget();
		}
	}

	/**
	 * 所在的activity退出时会将view从窗体分离, 即在该view销毁的时候调用,类似于activity的onDestory
	 * 所以可以加入取消广播注册等的操作
	 */
	@Override
	protected void onDetachedFromWindow() {
		scheduledExecutor.shutdown();
		super.onDetachedFromWindow();
	}

	/**
	 * 自带设配器,需要回调类来处理page的点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class ViewPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			// 将ImageView加入到ViewPager
			((ViewPager) arg0).addView(images.get(arg1));
			images.get(arg1).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onPagerClickCallback.onPagerClick();
				}
			});
			return images.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			// 将ImageView从ViewPager移除
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	/**
	 * 处理page点击的回调接口
	 * 
	 * @author lxj
	 * 
	 */
	interface OnPagerClickCallback {
		public abstract void onPagerClick();
	}
}
